package com.watermeter.android.utils;

import android.widget.Toast;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;
import java.util.Set;

/**
 * 网络请求工具
 * Created by zouqh on 2016/5/23.
 */
public class HttpHandler {
    String rels="";

    //用户登录
    public String HttpPost(Map<String,String> map,String url) {

        RequestParams params = new RequestParams(url);
        Set<String> keys = map.keySet();
        System.out.println("url=="+keys);
        for (String key : keys) {
                    params.addBodyParameter(key,  map.get(key));

        }

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                rels=result;
                Util.printLog("getMerchants------------onSuccess-----", result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");
                rels="error";
                Toast.makeText(x.app(), "连接失败！", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                rels="error";
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
            }

            @Override
            public void onFinished() {

            }
        });

        return rels;
    }
}
